<script lang="ts" setup>
import type { CheckboxValueType } from "element-plus";
import { handleTable_initData } from "@/pages/system/role/initData";

const { handle_permission, menu_permission, ifSuperAdmin } = defineProps({
  ifSuperAdmin: Boolean,
  handle_permission: {
    type: Array as () => Array<string>,
    default: () => [],
  },
  menu_permission: Array<any>,
  disEdit: {
    type: Boolean,
    default: true,
  },
});

const activeTab = ref("menu");
const handle_tableData = reactive(handleTable_initData);

const getAllPermissionList = (row: any) => {
  if (
    Array.isArray(row.AllPermissionList) &&
    row.AllPermissionList.length > 0
  ) {
    return row.AllPermissionList;
  }
  let AllPermissionList = [];
  if (!row.hideAdd) {
    AllPermissionList.push(`${row.entity}:add`);
  }
  if (!row.hideEdit) {
    AllPermissionList.push(`${row.entity}:edit`);
  }
  if (!row.hideDel) {
    AllPermissionList.push(`${row.entity}:del`);
  }
  if (!row.hideDetail) {
    AllPermissionList.push(`${row.entity}:detail`);
  }
  if (row.extra_handleList) {
    AllPermissionList.push(
      ...row.extra_handleList.map((item: any) => `${row.entity}:${item.code}`)
    );
  }
  row.AllPermissionList = AllPermissionList;
  return AllPermissionList;
};

const handleCheckAllChange = (val: CheckboxValueType, row: any) => {
  const { permissionList } = toRefs(row);
  permissionList.value = val
    ? row.AllPermissionList || getAllPermissionList(row)
    : [];
  row.isIndeterminate = false;
};

const handleCheckedChange = (value: CheckboxValueType[], row: any) => {
  let AllPermissionList_length = (
    row.AllPermissionList || getAllPermissionList(row)
  ).length;
  const checkedCount = value.length;
  row.checkAll = checkedCount === AllPermissionList_length;
  row.isIndeterminate =
    checkedCount > 0 && checkedCount < AllPermissionList_length;
};

// 反显操作权限
const init_handle_permission = (handle_permission: string[]) => {
  handle_tableData.forEach((item: any) => {
    item.permissionList = [];
    item.checkAll = false;
    item.isIndeterminate = false;
    let baseList = [];
    if (!item.hideAdd) {
      baseList.push(`${item.entity}:add`);
    }
    if (!item.hideEdit) {
      baseList.push(`${item.entity}:edit`);
    }
    if (!item.hideDel) {
      baseList.push(`${item.entity}:del`);
    }
    if (!item.hideDetail) {
      baseList.push(`${item.entity}:detail`);
    }
    baseList.forEach((item_temp: any) => {
      if (ifSuperAdmin) {
        item.permissionList.push(item_temp);
      } else {
        let index = handle_permission.indexOf(item_temp);
        if (index > -1) {
          item.permissionList.push(item_temp);
          handle_permission.splice(index, 1);
        }
      }
    });
    if (Array.isArray(item.extra_handleList)) {
      item.extra_handleList.forEach((item_temp: any) => {
        let target = `${item.entity}:${item_temp.code}`;
        if (ifSuperAdmin) {
          item.permissionList.push(target);
        } else {
          let index = handle_permission.indexOf(target);
          if (index > -1) {
            item.permissionList.push(target);
            handle_permission.splice(index, 1);
          }
        }
      });
    }
    getAllPermissionList(item);
    if (isEqualArray_simple(item.AllPermissionList, item.permissionList)) {
      item.checkAll = true;
    }
    if (
      item.permissionList.length > 0 &&
      item.permissionList.length < item.AllPermissionList.length
    ) {
      item.isIndeterminate = true;
    }
  });
};

onMounted(() => {
  if (handle_permission) {
    init_handle_permission(JSON.parse(JSON.stringify(handle_permission)));
  }
});
</script>
<template>
  <div>
    <el-tabs type="border-card" v-model="activeTab">
      <el-tab-pane label="菜单权限" name="menu">
        <el-tree
          :show-checkbox="!disEdit"
          :data="menu_permission"
          node-key="path"
          default-expand-all
          :expand-on-click-node="false"
          :indent="28"
        >
          <template #default="{ node, data }">
            <div class="custom-tree-node">
              <div
                class="flex items-center gap-2"
                :class="{
                  'font-bold': node.level === 1,
                  'text-4': node.level === 1,
                }"
              >
                <S-icon v-if="data.icon" :icon="data.icon" />
                <div>
                  {{ node.label }}
                </div>
              </div>
            </div>
          </template>
        </el-tree>
      </el-tab-pane>
      <el-tab-pane label="操作权限" name="handle">
        <el-table :data="handle_tableData" border>
          <el-table-column
            label="操作对象"
            prop="entityName"
            align="center"
            width="100"
          >
            <template #default="scope">
              <el-checkbox
                :disabled="disEdit"
                v-model="scope.row.checkAll"
                :indeterminate="scope.row.isIndeterminate"
                @change="handleCheckAllChange($event, scope.row)"
              >
                {{ scope.row.entityName }}
              </el-checkbox>
            </template>
          </el-table-column>
          <el-table-column label="操作方式" align="center">
            <template #default="scope">
              <el-checkbox-group
                v-model="scope.row.permissionList"
                @change="handleCheckedChange($event, scope.row)"
                :disabled="disEdit"
                class="flex flex-wrap"
              >
                <el-checkbox
                  v-if="!scope.row.hideAdd"
                  :label="scope.row.add_nickName || '新增'"
                  :value="`${scope.row.entity}:add`"
                />
                <el-checkbox
                  v-if="!scope.row.hideEdit"
                  :label="scope.row.edit_nickName || '编辑'"
                  :value="`${scope.row.entity}:edit`"
                />
                <el-checkbox
                  v-if="!scope.row.hideDel"
                  :label="scope.row.del_nickName || '删除'"
                  :value="`${scope.row.entity}:del`"
                />
                <el-checkbox
                  v-if="!scope.row.hideDetail"
                  label="详情"
                  :value="`${scope.row.entity}:detail`"
                />
                <el-checkbox
                  v-for="item in scope.row.extra_handleList"
                  :label="item.name"
                  :value="`${scope.row.entity}:${item.code}`"
                />
              </el-checkbox-group>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>
<style scoped>
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
:deep(.el-tree-node) {
  margin-top: 12px;
}
:deep(.el-checkbox__input.is-disabled.is-checked .el-checkbox__inner) {
  background-color: #409eff;
  border-color: #409eff;
}
:deep(.el-checkbox__input.is-disabled.is-checked .el-checkbox__inner:after) {
  border-color: white;
}
:deep(.el-checkbox__input.is-disabled.is-indeterminate .el-checkbox__inner) {
  background-color: #409eff;
  border-color: #409eff;
}
:deep(
    .el-checkbox__input.is-disabled.is-indeterminate .el-checkbox__inner:before
  ) {
  background-color: white;
}
</style>
